#!/usr/bin/env python
# encoding: utf-8
'''
poc.TJson -- shortdesc

poc.TJson is a description

It defines classes_and_methods

@author:     Anson Chan

@copyright:  2016 Xerland Co. Ltd. All rights reserved.

@contact:    anson_chan@xerland.com
@deffield    updated: Updated
'''
import sys
import json
from Utils.JsonHelper import toJson, fromJson
from Adb import MachModel
#from Adb.MachModel import MachModel
#import Adb
class TObj():
    def __init__(self, a, b):
        self.a = a
        self.b = b
    
    def __eq__(self,other):
        return (self.a == other.a and self.b==other.b)
        
    def getA(self):
        return self.a
    
    def getB(self):
        return self.b
        
#    def __repr__(self):
#        return '<TObj(%d,%d)>' %(self.a,self.b)
        
#    def toJson(self, obj):
#        print "ftw"
#        d = {'__class__':obj.__class__.__name__,
#             '__module__':obj.__module__}
#        d.update(obj.__dict__)
#        print d
#        return obj.__dict__
#    
#    def fromJson(self, d):
#        if '__class__' in d:
#            class_name = d.pop('__class__')
#            module_name = d.pop('__module__')
#            module = __import__(module_name)
#            print 'MODULE:', module
#            class_ = getattr(module, class_name)
#            print 'CLASS:', class_
#            args = dict( (key.encode('ascii'), value) for key, value in d.items())
#            print 'INSTANCE ARGS:', args
#            inst = class_(**args)
#        else:
#            inst = d
#        return inst    
    
#def toJson( obj):
#    d = {'__class__':obj.__class__.__name__,
#         '__module__':obj.__module__}
#    d.update(obj.__dict__)
#    return d
#class MyObj(object):
#    def __init__(self, s):
#        self.s = s
#    def __repr__(self):
#        return '<MyObj(%s)>' % self.s
#    
#obj = MyObj('instance value goes here')
#print 'First attempt'
#try:
#    print json.dumps(obj)
#except TypeError, err:
#    print 'ERROR:', err
#def convert_to_builtin_type(obj):
#    print 'default(', repr(obj), ')'
#    # Convert objects to a dictionary of their representation
#    d = { '__class__':obj.__class__.__name__,
#          '__module__':obj.__module__,
#        }
#    d.update(obj.__dict__)
#    return d
#print
#print 'With default'
#print json.dumps(obj, default=convert_to_builtin_type)    
#from Adb.MachModel import MachModel            
if __name__ == '__main__':
    to = TObj(1,2)
    tos = [to, to, to]
    print to.__dict__
    jss = json.dumps(tos, default=toJson)
    print jss
    ojs = json.loads(jss, object_hook = fromJson)
    print ojs, type(ojs)
    print ojs[0].a
    jstr = """[{"__module__": "Adb.MachModel", "PowerOnTime": null, "MachID": 1, "PowerOffTime": null, "__class__": "MachModel", "MachName": "xx"}]"""
    jsd = {'PowerOffTime': None, 'MachID': 1, 'PowerOnTime': None, 'MachName': u'xx'}
    m = __import__("Adb.MachModel",  fromlist=["MachModel"])
#    from Adb.MachModel import MachModel
    print m
    a = getattr(m, "MachModel")
    print a
#    a(**jsd)
#    print MachModel(**jsd)
    ojss = json.loads(jstr, object_hook = fromJson)
    print ojss
#    mach = MachModel(1,"mach")
#    machjstr = json.dumps(mach, default=toJson)
#    print machjstr
    